#include "df.hpp"
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <sstream>

/*********** main ***********/
int main(int argc, char* argv[]){
  if(argc < 2){
    cerr << "Utilisation :"<< endl;
    cerr << argv[0] << " -closure fichier 'attributs à fermer'" << endl;
    cerr << argv[0] << " -models fichier df" << endl;
    cerr << argv[0] << " -closurelin fichier atts" << endl;    
    cerr << argv[0] << " -generate i" << endl;    
    cerr << "utiliser '-' à la place de fichier pour lire depuis l'entrée standard" << endl;
    return EXIT_FAILURE;
  }

  string options[] = {"-closure", "-models", "-closurelin", "-generate"};
  string opt(argv[1]);
  string* p = find(options, options + 5, opt);
  
  string file;
  data_t atts;
  df_t f;
  dfs_t s, r;
  stringstream ss;
  index_t idx;
  size_t n;
  
  switch (p - options) {
    case 0:
      file = argv[2];
      atts = load_atts(argv[3]);
      (file == "-") ? load_dfs(cin,s) : load_dfs(file,s);
      cout << closure(s,atts) << endl;
      break;
    case 1:
      file = argv[2];
      ss << argv[3];
      ss >> f;
      (file == "-") ? load_dfs(cin,s) : load_dfs(file,s);
      cout << models(s,f) << endl;
      break;
    case 2:
      file = argv[2];
      atts = load_atts(argv[3]);
      (file == "-") ? load_dfs(cin,s) : load_dfs(file,s);
      idx = buildindex(s);
      cout << closurelin(s,atts,idx) << endl;
      break;
    case 3:
      ss << argv[2];
      ss >> n;
      s = generate(n);
      cout << s << endl;
      break; 
    default:
      cerr << "Option '" << opt << "' inconnue" << endl;
      return EXIT_FAILURE;
  }

  return EXIT_SUCCESS;
}
